/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logica.Diagnostico;
import Logica.Reparacion;
import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Pablo
 */
public class ReparacionJpaController implements Serializable {
    
    
    public ReparacionJpaController(){
        emf = Persistence.createEntityManagerFactory("TpFinalPU");
    }


    public ReparacionJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reparacion reparacion) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Diagnostico unDiagnostico = reparacion.getUnDiagnostico();
            if (unDiagnostico != null) {
                unDiagnostico = em.getReference(unDiagnostico.getClass(), unDiagnostico.getNroDiagnostico());
                reparacion.setUnDiagnostico(unDiagnostico);
            }
            em.persist(reparacion);
            if (unDiagnostico != null) {
                Reparacion oldUnaReparacionOfUnDiagnostico = unDiagnostico.getUnaReparacion();
                if (oldUnaReparacionOfUnDiagnostico != null) {
                    oldUnaReparacionOfUnDiagnostico.setUnDiagnostico(null);
                    oldUnaReparacionOfUnDiagnostico = em.merge(oldUnaReparacionOfUnDiagnostico);
                }
                unDiagnostico.setUnaReparacion(reparacion);
                unDiagnostico = em.merge(unDiagnostico);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReparacion(reparacion.getNroReparacion()) != null) {
                throw new PreexistingEntityException("Reparacion " + reparacion + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reparacion reparacion) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reparacion persistentReparacion = em.find(Reparacion.class, reparacion.getNroReparacion());
            Diagnostico unDiagnosticoOld = persistentReparacion.getUnDiagnostico();
            Diagnostico unDiagnosticoNew = reparacion.getUnDiagnostico();
            if (unDiagnosticoNew != null) {
                unDiagnosticoNew = em.getReference(unDiagnosticoNew.getClass(), unDiagnosticoNew.getNroDiagnostico());
                reparacion.setUnDiagnostico(unDiagnosticoNew);
            }
            reparacion = em.merge(reparacion);
            if (unDiagnosticoOld != null && !unDiagnosticoOld.equals(unDiagnosticoNew)) {
                unDiagnosticoOld.setUnaReparacion(null);
                unDiagnosticoOld = em.merge(unDiagnosticoOld);
            }
            if (unDiagnosticoNew != null && !unDiagnosticoNew.equals(unDiagnosticoOld)) {
                Reparacion oldUnaReparacionOfUnDiagnostico = unDiagnosticoNew.getUnaReparacion();
                if (oldUnaReparacionOfUnDiagnostico != null) {
                    oldUnaReparacionOfUnDiagnostico.setUnDiagnostico(null);
                    oldUnaReparacionOfUnDiagnostico = em.merge(oldUnaReparacionOfUnDiagnostico);
                }
                unDiagnosticoNew.setUnaReparacion(reparacion);
                unDiagnosticoNew = em.merge(unDiagnosticoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = reparacion.getNroReparacion();
                if (findReparacion(id) == null) {
                    throw new NonexistentEntityException("The reparacion with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reparacion reparacion;
            try {
                reparacion = em.getReference(Reparacion.class, id);
                reparacion.getNroReparacion();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reparacion with id " + id + " no longer exists.", enfe);
            }
            Diagnostico unDiagnostico = reparacion.getUnDiagnostico();
            if (unDiagnostico != null) {
                unDiagnostico.setUnaReparacion(null);
                unDiagnostico = em.merge(unDiagnostico);
            }
            em.remove(reparacion);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reparacion> findReparacionEntities() {
        return findReparacionEntities(true, -1, -1);
    }

    public List<Reparacion> findReparacionEntities(int maxResults, int firstResult) {
        return findReparacionEntities(false, maxResults, firstResult);
    }

    private List<Reparacion> findReparacionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Reparacion.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reparacion findReparacion(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reparacion.class, id);
        } finally {
            em.close();
        }
    }

    public int getReparacionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Reparacion> rt = cq.from(Reparacion.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
